package stock;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Iterator;

import javax.swing.JOptionPane;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import stock.Company;

public class ScreenScrapper {

	private static String dayUrl = "http://economia.terra.com.ar/mercados/acciones/default.aspx?idtel=IB031MERVAL&amp;col=0";
	private static String weekUrl = "http://economia.terra.com.ar/mercados/acciones/ultimos.aspx?idtel=IB031MERVAL&col=0";
	
	private static String dayMervalUrl = "http://www.merval.sba.com.ar/default.aspx";
	
	public ScreenScrapper(){
		
	}
	
	public static Company gatherMervalInformation(){
		try{

			String name = "Merval";
			Document doc = getMervalDocument();
	        String last = doc.select("span#ctl00_ContentCentral_GraficoIndiceHome1_lblUltimo").get(0).text().replace(".", "");
	        String variation = doc.select("span#ctl00_ContentCentral_GraficoIndiceHome1_lblVariacion").get(0).text();
	        
	        //Ver como conseguir esta info
	        String percentualVariation = "0";
	        String volume = "0";
	        
	        ArrayList<String> mervalInfo = new ArrayList<>();
	        mervalInfo.add(name);
	        mervalInfo.add(last);
	        mervalInfo.add(variation);
	        mervalInfo.add(percentualVariation);
	        mervalInfo.add(volume);
	        
	        Company merval = new Company(mervalInfo);
	        
	        return merval;
	        
		}catch (Exception e) {
			JOptionPane.showMessageDialog(null, "Screenscrapping could not be acomplished.", "Scrapping error", JOptionPane.ERROR_MESSAGE);
		}
		return null;
	}
	
	public static ArrayList<Company> gatherCompaniesInformation() {
		
		try{
			ArrayList<Company> companies = new ArrayList<Company>();
			
			//Day Information
			Document doc = getDayCompanyDocument();
	        Element table = doc.select("div#htmlBuffer > table").get(0);
	        Elements tbody = table.select("tbody");
	        Iterator it = tbody.iterator();
	        while (it.hasNext()){
	        	ArrayList<String> companyInfo = new ArrayList<String>();
	        	
	        	Element tbAux = (Element)it.next();
	        	Element name = tbAux.select("tr").get(0).select("th > a").get(0);
	        	companyInfo.add(name.text());
	        	
	        	Elements information = tbAux.select("td");
	        	Iterator tdIter = information.iterator();
	        	while (tdIter.hasNext()){
	        		Element tdAux = (Element)tdIter.next();
	        		companyInfo.add(tdAux.text());
	        	}

	        	companies.add(new Company(companyInfo));
	        }
	        
	        //Week Information
	        doc = getWeekCompanyDocument();
	        table = doc.select("div#htmlBuffer > table").get(0);
	        Elements tr = table.select("tbody").get(0).select("tr");
	        it = tr.iterator();
	        while (it.hasNext()){
	        	Element trAux = (Element)it.next();
	        	String name = trAux.select("th > a").get(0).text();
	        	int companyIndex = containsCompany(companies, name);
	        	if ( companyIndex != -1 ){
	        		ArrayList<String> weekInfo = new ArrayList<String>();
	        		Elements tdValues = trAux.select("td");
	        		for ( Element td : tdValues ){
	        			weekInfo.add(td.text());
	        		}
	        		companies.get(companyIndex).setWeekInfo(weekInfo);
	        	}
	        }
	        
	        return companies;
			
			
		} catch (Exception e) {
			JOptionPane.showMessageDialog(null, "Screenscrapping could not be acomplished.", "Scrapping error", JOptionPane.ERROR_MESSAGE);
		}
		return null; 
	}
	
	private static Document getDayCompanyDocument() throws IOException {
		//Document doc = Jsoup.connect(dayUrl).get();
		Document doc = Jsoup.parse(new File("htmlPages/dayCompany.html"), "UTF-8");
        return doc;
	}

	private static Document getWeekCompanyDocument() throws IOException {
		//Document doc = Jsoup.connect(weekUrl).get();
        Document doc = Jsoup.parse(new File("htmlPages/weekCompany.html"), "UTF-8");
        return doc;
	}
	
	private static Document getMervalDocument() throws IOException {
		//Document doc = Jsoup.connect(dayMervalUrl).get();
        Document doc = Jsoup.parse(new File("htmlPages/mervalInfo.html"), "UTF-8");
        return doc;
	}
	
	private static int containsCompany(ArrayList<Company> companies,String name){
		for ( Company company : companies ){
			if ( company.getName().equals(name) ){
				return companies.indexOf(company);
			}
		}
		return -1;
	}
}
